/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fetcher;

import Database.*;

import jWASA.PHR;
import jWASA.Sample;
import jWASA.Sensor;
import jWASA.User;
import java.util.Collection;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author gianmariospacagna
 */
public class Fetcher extends Thread {

    Collection<Sample> samples;
    Collection<PHR> patientRecords;
    EntityManagerFactory emf;
    EntityManager em;
    Boolean stop;

    public Fetcher(Collection<Sample> samples, Collection<PHR> patientRecords) {
        this.samples = samples;
        this.patientRecords = patientRecords;
        this.emf = javax.persistence.Persistence.createEntityManagerFactory("UbiSalusServerPU");
        this.em = emf.createEntityManager();
        this.stop = false;
    }

    @Override
    public void run() {

        System.out.print("Fetcher: started");
        while (true) {
            while (!stop) {


                EntitySample eSam;
                EntityPHR ePhr;
                EntitySensor eSen;
                EntityUser eUser;
                try {
                    for (Sample s : samples) {
                        eSam = matchSample(s);
                        System.out.println("Fetcher: New Sample found " + s.getTimestamp() + " " + s.getValues());

                        persist(eSam);
                        samples.remove(s);

                    }

                    for (PHR phr : patientRecords) {
                        ePhr = matchPhr(phr);
                        try {
                            System.out.println("Fetcher: New PHR found " + phr.getUser() + " " + phr.getDate() + " " + phr.getTime());

                            persist(ePhr);
                            patientRecords.remove(phr);
                        } catch (Exception e) {
                            //do nothing
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    System.err.println("Fetcher-> Error writing in the DB");

                }


            }
        }
    }

    public void persist(Object object) {

        if (!em.getTransaction().isActive()) {
            em.getTransaction().begin();
        }


        try {
            //em.persist((EntitySample) object);
            em.merge(object);
            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            //em.close();
        }
    }

    private EntitySensor matchSensor(Sensor sensor) {
        EntitySensor eSen = new EntitySensor();
        eSen.setName(sensor.getName());
        eSen.setDim(sensor.getDim());
        eSen.setDescription(sensor.getDescription());
        eSen.setWasaId(sensor.getWasaId());
        return eSen;
    }

    private EntitySample matchSample(Sample s) {
        EntitySample eSam = new EntitySample();
        eSam.setTimestamp(s.getTimestamp());
        eSam.setSensor(matchSensor(s.getSensor()));
        //eSam.setValues(s.getValues());
        eSam.setVal(s.getValues()[0]);
        return eSam;
    }

    private EntityPHR matchPhr(PHR phr) {
        EntityPHR ePhr = new EntityPHR();
        ePhr.setDateDay(phr.getDate());
        ePhr.setDateTime(phr.getTime()) ;
        ePhr.setPlace(phr.getPlace());
        ePhr.setRoutineDescription(phr.getRoutineDescription());
        ePhr.setSyntompsDescription(phr.getSyntompsDescription());        
        ePhr.setTags(phr.getTags());
        ePhr.setUsername(phr.getUser());
        //ePhr.setUser(matchUser(phr.getUser()));
        return ePhr;
    }

    private EntityUser matchUser(User user) {
        EntityUser eUser = new EntityUser();
        eUser.setUsername(user.getUsername());
        eUser.setName(user.getName());
        eUser.setSurname(user.getSurname());
        eUser.setPhone(user.getPhone());
        return eUser;
    }

    public void safeStop() {
        stop = true;
        System.out.println("Fetcher: stopped");
    }

    public void safeResume() {
        stop = false;
        System.out.println("Fetcher: Resumed");
    }
}
